package design;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.util.StringTokenizer;

//import java.util.List;
//import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class SumReducer1 extends Reducer
        <Text, IntWritable, Text, IntWritable> {
    /*统计每个属性的取值记录数*/
    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context) throws IOException, InterruptedException {
        System.out.println("reduce");
        IntWritable result = new IntWritable();
        int sum = 0;
        String line = key.toString();
        StringTokenizer itr = new StringTokenizer(line);
        for (IntWritable value : values) {
            sum += value.get();
        }
        result.set(sum);
        writeToFile(key + " " + sum);//JOB1的输出结果写入本地文件便于程序调试
        context.write(key, result);//no_attr+attr_value+classlabel作为key
        //取值记录数作为value
    }

    public static void writeToFile(String text) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/Users/kami/C4.5Parallelization/result/JOB1/intermediate" + C45.current_index + ".txt"), true));
            bw.write(text);
            bw.newLine();
            bw.close();
        } catch (Exception e) {
        }
    }
}








